/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Comment;
import pojo.Sanpham;
import pojo.Taikhoan;
import util.HibernateUtil;

/**
 *
 * @author WMThiet
 */
public class CommentDAO {
    public static boolean  ThemBinhLuanMoi(Sanpham sp,Taikhoan tk,String noiDung) {
        boolean bDone = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            session.beginTransaction();
            
            Comment bl = new Comment();
            bl.setSanpham(sp);
            bl.setTaikhoan(tk);
            bl.setNoiDungComment(noiDung);
            bl.setNgayComment(new Date());
            bl.setDaXoa(false);
            
            session.save(bl);            
            session.getTransaction().commit();     
            
            bDone = true;
        }catch(Exception ex){
            session.getTransaction().rollback();
            bDone = false;
        }finally{
            session.close();
        }
        
        return bDone;
    }
    
    public static List<Comment> LayBinhLuan(int maSP,int soSPTrenTrang,int trangHienTai){
        List<Comment> listBL = null;  
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            session.beginTransaction();
            
            String hql = "from Comment bl where bl.daXoa =:dx and bl.sanpham.maSanPham=:maSP order by bl.ngayComment desc";
            Query query = session.createQuery(hql);
            query.setInteger("maSP", maSP);
            query.setBoolean("dx", false);
            
            if(soSPTrenTrang != -1){
                query.setFirstResult((trangHienTai - 1)*soSPTrenTrang);
                query.setMaxResults(soSPTrenTrang);
            }
            listBL = query.list();
            
        }catch(Exception ex){
             listBL = null;
        }finally{
            session.close();
        }
        
        return listBL;
    }
    
    public static int TinhSoTrangHienThi(int maSP,int soSpTren1Trang){
        
        if(soSpTren1Trang == -1){
            return 1;
        }
        
        int soTrang = 0;
        int tongSoTrang = 0;        
        
        List<Comment> listBL = LayBinhLuan(maSP, -1, 1);        
        if(listBL != null){
            tongSoTrang = listBL.size();
        }
        
        if(tongSoTrang == 0){
            return 0;
        }
        
        soTrang = tongSoTrang/soSpTren1Trang;
        
        if(tongSoTrang % soSpTren1Trang != 0){
            soTrang ++;
        }
        
        return soTrang;
    }
    
    public static int TinhTongBinhLuan(int maSP) {
        int tongBL = 0;
        List<Comment> listBinhLuan = LayBinhLuan(maSP, -1, 1);
        if(listBinhLuan != null){
            tongBL = listBinhLuan.size();
        }
        
        return tongBL;
    }
}
